package ftp

import (
	"fmt"
	"gin/model"
	"strconv"
	"strings"
	"time"
)

// 管理员日志
func (*DataParsing) parsingBunkerLog(catData []string, ftpInfoId int64) []model.DataBunker {
	var logDate, LogTime, dataType, steamId, gameName, gameId, boxType, location string
	var area, bunkerType string
	var end int
	var dataArr []model.DataBunker
	for _, logTxt := range catData {

		boxType = ""
		steamId = ""
		gameName = ""
		gameId = ""
		boxType = ""
		location = ""
		//fmt.Println("原始数据", logTxt)

		parts := strings.Split(logTxt, ":")
		//fmt.Println("分割数据", parts)
		timeParts := strings.Split(parts[0], "-")
		//fmt.Println("分割数据2", timeParts)
		//日期转换  年-月-日
		logDate = strings.Replace(timeParts[0], ".", "-", -1)
		//fmt.Println("转换数据1", catDate)
		//时间转换  时:分:秒
		Time := strings.Replace(timeParts[1], ".", ":", -1)
		//fmt.Println("转换数据2", catTime)
		layout := "2006-01-02 15:04:05"
		// 解析时间字符串
		//fmt.Println("解析时间格式为", catDate+" "+catTime)
		t, err := time.Parse(layout, logDate+" "+Time)
		if err != nil {
			fmt.Println("解析时间格式为", err.Error())
			LogTime = "0"
			//return
		}
		// 转换为 Unix 时间戳（秒）

		LogTime = strconv.FormatInt(t.Unix(), 10)
		logTime, _ := strconv.ParseInt(LogTime, 10, 64)
		//太平洋时间制  + 8 小时
		//logTime = logTime + 3600*8
		t = time.Unix(logTime, 0)
		logDate = t.Format("2006-01-02")

		hour, minute, second := t.Clock()
		Time = fmt.Sprintf("%02d:%02d:%02d", hour, minute, second)

		//开锁类型过滤
		start := strings.Index(logTxt, ": [")
		if start < 0 {
			dataType = ""
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+3:], "]")
			if end < 0 {
				dataType = ""
			} else {
				dataType = logTxt[start+3 : start+3+end]
			}
		}

		if dataType != "LogBunkerLock" {
			//不是地堡日志跳过
			continue
		}

		//获取箱子id
		start = strings.Index(logTxt, "] ")
		//fmt.Println("位置1", start)
		if start < 0 {
			area = ""
		} else {
			end = strings.Index(logTxt[start+4:], " Bunker")
			if end < 0 {
				area = ""
			} else {
				area = logTxt[start+4 : start+4+end]
			}
		}

		if area != "" {
			//不是区域数据跳过
			continue
		}

		//获取数据类型
		start = strings.Index(logTxt, "Bunker ")
		//fmt.Println("位置1", start)
		if start < 0 {
			bunkerType = ""
		} else {
			end = strings.Index(logTxt[start+4:], ".")
			if end < 0 {
				bunkerType = ""
			} else {
				bunkerType = logTxt[start+4 : start+4+end]
			}
		}
		//如果数据为空则第二种类型取数据
		if bunkerType == "" {
			//获取数据类型
			start = strings.Index(logTxt, "Bunker ")
			//fmt.Println("位置1", start)
			if start < 0 {
				bunkerType = ""
			} else {
				end = strings.Index(logTxt[start+4:], " ")
				if end < 0 {
					bunkerType = ""
				} else {
					bunkerType = logTxt[start+4 : start+4+end]
				}
			}
		}

		//LogBunkerLock

		start = strings.Index(logTxt, "tion: ")
		if start < 0 {
			location = ""
		} else {
			// 查找第二个单引号的位置
			location = logTxt[start+6:]
		}

		//fmt.Println("数据为：", ftpInfoId, logDate, Time, LogTime, steamId, gameName, gameId, location)

		gameId, _ := strconv.ParseInt(gameId, 10, 64)

		downloadFile := model.DataBunker{FtpInfoId: ftpInfoId, Date: logDate, Time: Time, LogTime: logTime,
			SteamId: steamId, GameName: gameName, GameId: gameId, Type: boxType, Location: location}
		dataArr = append(dataArr, downloadFile)
	}
	return dataArr
}
